package com.example.finalhomework.thirdhomework

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.finalhomework.R

@Composable
fun SecondScreen_3(navController: NavHostController, message: String) {
    Box(
        modifier = Modifier
            .fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        Image(
            painter = painterResource(id = R.drawable.background3),
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier.fillMaxHeight()
        )
        Column(
            modifier = Modifier
                .fillMaxSize() // 填充整个屏幕
                .padding(16.dp), // 设置内边距为16dp
            verticalArrangement = Arrangement.Top, // 垂直居中排列
            horizontalAlignment = Alignment.CenterHorizontally // 水平居中排列
        ) {

            Spacer(modifier = Modifier.height(32.dp))
            // 按钮，点击后返回到 MainScreen
            Button(onClick = {
                navController.popBackStack("thirdhomework", false) // 返回到 thirdhomework，不销毁当前目的地
                Log.d("SecondScreen_3", "Back to Main Screen button clicked")
            }) {
                Text("Back to Main Screen") // 按钮上的文本
            }
            Spacer(modifier = Modifier.height(16.dp)) // 间隔16dp的空白
            // 显示传递过来的消息
            Text(text = if (message.isEmpty()) "There is no message passed here" else message)
            Log.d("SecondScreen_3", "Displayed message: $message")
        }
    }
}

@Preview
@Composable
fun PreviewSecondScreen_3() {
    SecondScreen_3(navController = rememberNavController(), message = "Hello from Main Screen")
}